package com.sanpu.kxe.business.main.view;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.utils.FileUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.library.imagechoose.utils.PermissionsUtils;

import java.io.File;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：授权书
 * Created by 薛勇军 on 2019/9/29.
 */

public class AuthorizationActivity extends BaseTitleActivity {

    @Bind(R.id.merchantCode)
    TextView vMerchantCode;
    @Bind(R.id.contentTv)
    TextView vContentTv;
    @Bind(R.id.chooseLl)
    LinearLayout chooseImageIv;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_authorization;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("授权书");
        vMerchantCode.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null && !TextUtils.isEmpty(centerOutput.getMerchantCode())) {
            vMerchantCode.setText(centerOutput.getMerchantCode());
        }
        String userName = "";
        if (centerOutput != null && !TextUtils.isEmpty(centerOutput.getMerchantCode())) {
            userName = centerOutput.getCrpNm();
        }
        String content = "　　经审核" + userName + "符合“银信通”的准入要求，特授权其为银信通产品合作伙伴，允许使用银信通APP进行合规展业。详细授权内容以合作协议为准。";
        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new UnderlineSpan(), 5, userName.length() + 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        vContentTv.setText(spannableString);
    }

    @OnClick({R.id.saveSystemLl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.saveSystemLl:
                chooseImageIv.destroyDrawingCache();
                chooseImageIv.setDrawingCacheEnabled(true);
                Bitmap mClipScreen = chooseImageIv.getDrawingCache();
                if (mClipScreen == null) return;
                if (!PermissionsUtils.checkWriteStoragePermission(mContext))
                    return;
                File result = FileUtil.saveShareImageToGallery(mContext, mClipScreen);
                ToastUtil.showTextViewPrompt(result != null ? "图片保存成功!请在相册查看" : "图片保存失败");
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }
}
